package com.moleq.adapter;

import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.itemsetting.tools.MGCreateFiles;
import com.moleq.mgpos.activities.FlipChartSettingActivity;
import com.moleq.models.FlipChart;
import com.moleq.models.GlobalInfo;
import com.moleq.mgpos.activities.R;
import com.moleq.service.FlipChartService;
import com.moleq.service.ServiceManager;

public class FlipChartAdapter extends BaseAdapter {
	
	private LayoutInflater mInflater;
	private List<FlipChart> listFC;
//	private List<FlipChart> listflipChart = null;
	private int selectedRow = 0;
	public FlipChartSettingActivity fcsa;
	private ViewHolder holder = null;
	private View ButtonView = null;
	private View ImageButtonView = null;
	private Button btn_dialog_ok,btn_dialog_cancel;
	private EditText et_dialog_order;
	private TextView tv_old_order, tv_title;
	private FlipChartService fcService;
	private LinearLayout ll_delete;
	private int oldOrder;
	private int oldGroup;
	private int oldID;
	private int deleteID;
	private int deleteOrder;	
	private MessageDialog mMessageDialog;
	private boolean result = false;
	
	private MGCreateFiles mMGCreateFiles = new MGCreateFiles();
	
	public FlipChartAdapter(Context context,List<FlipChart> listFC){
		this.mInflater = LayoutInflater.from(context);
		this.listFC = listFC;
		fcsa = (FlipChartSettingActivity) context;
//		fcService = new FlipChartService(context);
		fcService = ServiceManager.getInstance().mFlipChartService;
	}
	
	public void setSource(List<FlipChart> listFC){
		this.listFC = listFC;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return listFC.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listFC.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parents) {
		// TODO Auto-generated method stub
		
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.fc_item_list, null);
			holder.tv_id = (TextView) convertView.findViewById(R.id.tv_id);
			holder.tv_group = (TextView) convertView.findViewById(R.id.tv_group);
			holder.btn_order =  (Button) convertView.findViewById(R.id.btn_order);
			holder.tv_upc = (TextView) convertView.findViewById(R.id.tv_upc);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			holder.ibtn_delete = (ImageButton) convertView.findViewById(R.id.ibtn_delete);
			holder.ll_delete = (LinearLayout) convertView.findViewById(R.id.ll_delete);
			holder.ll_order = (LinearLayout) convertView.findViewById(R.id.ll_order);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
				
		holder.tv_id.setText(String.valueOf(listFC.get(position).getId()));
		holder.tv_group.setText(String.valueOf(listFC.get(position).getGroup()));
		holder.btn_order.setText(String.valueOf(position + 1));
		holder.btn_order.setTag(String.valueOf(listFC.get(position).getId()));
		holder.tv_upc.setText(String.valueOf(listFC.get(position).getUpc()));
		if (listFC.get(position).getName().length() > 18) {
			holder.tv_name.setText(String.valueOf(listFC.get(position).getName().substring(0,17)));
		}else {
			holder.tv_name.setText(String.valueOf(listFC.get(position).getName()));		
		}
//		holder.ibtn_delete.setTag(String.valueOf(listFC.get(position).getId()));
		holder.ibtn_delete.setTag(String.valueOf(listFC.get(position).getId()) + "|" 
				+ String.valueOf(listFC.get(position).getOrder()));
		
		if (position == selectedRow) {
			holder.tv_upc.setBackgroundColor(Color.rgb(255,219,16));
			holder.tv_name.setBackgroundColor(Color.rgb(255,219,16));
			holder.ll_delete.setBackgroundColor(Color.rgb(255, 219, 16));
			holder.ll_order.setBackgroundColor(Color.rgb(255, 219, 16));
		} else {
			holder.tv_upc.setBackgroundColor(Color.rgb(204, 204, 204));
			holder.tv_name.setBackgroundColor(Color.rgb(204, 204, 204));
			holder.ll_delete.setBackgroundColor(Color.rgb(204, 204, 204));
			holder.ll_order.setBackgroundColor(Color.rgb(204, 204, 204));
		}
		
		holder.btn_order.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ButtonView = v;
				ScrollToRow(position);
				oldGroup = Integer.valueOf(holder.tv_group.getText().toString());
				oldID = listFC.get(position).getId();
				
				DialogWindows();
			}
		});
		
		holder.ibtn_delete.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				ImageButtonView = v;
				ImageButton ib = (ImageButton) ImageButtonView.findViewById(R.id.ibtn_delete);
				String temp = ib.getTag().toString();
				String[] strs = temp.split("[|]");
//				final int id = Integer.valueOf(strs[0].toString());
//				final int order = Integer.valueOf(strs[1].toString());
				deleteID = Integer.valueOf(strs[0].toString());
				deleteOrder = Integer.valueOf(strs[1].toString());
				oldGroup = Integer.valueOf(holder.tv_group.getText().toString());
				
				ScrollToRow(position);
				Message("Do you want to delete the item?",
							mMessageDialog.MESSAGE_BOX_YES_NO,mMessageDialog.ICON_INFO);
				
			}
		});
		
		return convertView;
	}
	
	Dialog DialogWindow = null;
	public void DialogWindows(){
		if (DialogWindow == null) {
			DialogWindow = new Dialog(fcsa, R.style.Transparent_dialog);
			DialogWindow.setContentView(R.layout.fc_dialog_perview_models);
			Window win = DialogWindow.getWindow();
			WindowManager.LayoutParams lp = win.getAttributes();
			lp.x= -235;			 
			lp.y= -50;
		}
		tv_old_order = (TextView) DialogWindow.findViewById(R.id.tv_old_order);
		Button b = (Button) ButtonView.findViewById(R.id.btn_order);
		oldOrder = Integer.valueOf(b.getText().toString());
		
		tv_old_order.setText(b.getText().toString());
		tv_title = (TextView) DialogWindow.findViewById(R.id.tv_title);
		et_dialog_order =  (EditText) DialogWindow.findViewById(R.id.et_dialog_order);
		et_dialog_order.setText("");
		btn_dialog_ok = (Button) DialogWindow.findViewById(R.id.btn_dialog_ok);
		btn_dialog_cancel = (Button) DialogWindow.findViewById(R.id.btn_dialog_cancel);
		
		btn_dialog_ok.setOnClickListener(OnClickDialogWindowListener);
		btn_dialog_cancel.setOnClickListener(OnClickDialogWindowListener);
		
		DialogWindow.show();
	}

	OnClickListener OnClickDialogWindowListener = new OnClickListener() {
		
		public void onClick(View view) {
			// TODO Auto-generated method stub
			switch (view.getId()) {
			case R.id.btn_dialog_ok:
				et_dialog_order = (EditText) DialogWindow.findViewById(R.id.et_dialog_order);
				String sOrder = et_dialog_order.getText().toString();
				if (sOrder.equals("0")) {
					Message("Please enter a valid order.",
							mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_INFO);
					return;
				}
				if (!sOrder.equals("")) {
					int newOrder = Integer.valueOf(sOrder);
					int maxOrder = fcService.getMaxOrder();
					if (newOrder > maxOrder) {
						newOrder = maxOrder;
					}
					Button b = (Button) ButtonView.findViewById(R.id.btn_order);
					b.setText(String.valueOf(newOrder));
					
					result = fcService.updateOrder(newOrder, oldID);
					if (GlobalInfo.getInstance().isMGNetwork) {
						FlipChart flipChartx = fcService.FindFlipChartByID(oldID);
						mMGCreateFiles.createrFile(flipChartx.toMGData("UPDATE"));
					}
					if (result) {
						if (GlobalInfo.getInstance().isMGNetwork) {
							if (newOrder < oldOrder) {
								for (int i = newOrder - 1; i < oldOrder - 1; i++) {
									FlipChart flipChartx = listFC.get(i);
									flipChartx.setOrder(flipChartx.getOrder() + 1);
									mMGCreateFiles.createrFile(listFC.get(i).toMGData("UPDATE"));
								}
							}else if (newOrder > oldOrder) {
								for (int i = newOrder - 1; i >= oldOrder; i--) {
									FlipChart flipChartx = listFC.get(i);
									flipChartx.setOrder(flipChartx.getOrder() - 1);
									mMGCreateFiles.createrFile(listFC.get(i).toMGData("UPDATE"));
								}
							}
						}
						fcService.postUpdateSetOrder(newOrder, oldOrder, oldID, oldGroup);
						fcsa.ShowMessage(result);			
						listFC = fcService.getAllFlipChart();
						setSource(listFC);
						notifyDataSetChanged();
						ScrollToRow(newOrder - 1);
						FlipChartSettingActivity.set_lv_chart_Selection(newOrder - 1);
					}
				}
				fcsa.Keyboard();
				DialogWindow.dismiss();
				break;
			case R.id.btn_dialog_cancel:
				fcsa.Keyboard();
				DialogWindow.dismiss();
				break;
			default:
				break;
			}
		}
	};
	
	public final class ViewHolder{
		public TextView tv_group;
		public TextView tv_id;
		public Button btn_order;
		public EditText et_order;
		public TextView tv_upc;
		public TextView tv_name;
		public ImageButton ibtn_delete;
		public LinearLayout ll_delete;
		public LinearLayout ll_order;
	}
	public void ScrollToRow(int row){
		selectedRow = row;
		this.notifyDataSetChanged();
	}
	public int GetSelectedIndex(){
		return selectedRow;
	}
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener(){

		public void onClickYes() {
			// TODO Auto-generated method stub
			int index = GetSelectedIndex();
			result = fcService.deleteFCById(deleteID);
			if (GlobalInfo.getInstance().isMGNetwork) {
				mMGCreateFiles.createrFile(listFC.get(index).toMGData("DELETE"));
			}
			if (result) {
				fcService.postDeleteSetOrder(deleteOrder,oldGroup);
				if (GlobalInfo.getInstance().isMGNetwork) {
					for (int i = index + 1; i < listFC.size(); i++) {
						FlipChart fc = listFC.get(i);
						fc.setOrder(fc.getOrder() - 1);
						mMGCreateFiles.createrFile(fc.toMGData("UPDATE"));
					}
				}
				fcsa.showDeleteMessage(result);
				listFC = fcService.getAllFlipChart();
				setSource(listFC);
				notifyDataSetChanged();
			}
			mMessageDialog.dismiss();
		}

		public void onClickNo() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}

		public void onClickOK() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}
		
	};
	
	public void Message(String text,int type,int icon){
		mMessageDialog = new MessageDialog(fcsa);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}
}
